﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Inspector.cs" company="Marcelo Roca">
//   Copyright (c) 2011 Marcelo Roca. All rights reserved.
// </copyright>
// <author name="Marcelo Roca" email="marce@marceloroca.com"/>
// <license>
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
// 
//   This program is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program.  If not, see http://www.gnu.org/licenses/.
// </license>
// --------------------------------------------------------------------------------------------------------------------

namespace GameKits.Unity.SmartInspector.Editor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using UnityEditor;

    using UnityEngine;

    /// <summary>
    /// Es un inspector para el componente
    /// </summary>
    public class Inspector
    {
        /// <summary>
        /// La lista de inspectors, cada gameObject tiene un inspector
        /// </summary>
        private static readonly Dictionary<GameObject, Inspector> InspectorList = new Dictionary<GameObject, Inspector>();

        /// <summary>
        /// El gameObject que está vinculado a est inspector
        /// </summary>
        private readonly GameObject gameObject;

        /// <summary>
        /// El EditorWindow que tiene este inspector
        /// </summary>
        private readonly InspectorWindow inspectorWindow;

        /// <summary>
        /// la lsta de componentes que están en este inspector
        /// </summary>
        private Dictionary<MonoBehaviour, ComponentEditor> componentList;

        /// <summary>
        /// La posición del scroll
        /// </summary>
        private Vector3 scrollPosition;

        /// <summary>
        /// Initializes a new instance of the <see cref="Inspector"/> class.
        /// </summary>
        /// <param name="gameObject">
        /// The game object.
        /// </param>
        /// <param name="inspectorWindow">
        /// The inspector window.
        /// </param>
        /// <exception cref="ArgumentException">
        /// </exception>
        public Inspector(GameObject gameObject, InspectorWindow inspectorWindow)
        {
            if (InspectorList.ContainsKey(gameObject))
            {
                throw new ArgumentException("El gameObject ya existe", "gameObject");
            }

            this.gameObject = gameObject;
            this.UpdateComponentList();
            InspectorList.Add(gameObject, this);
            this.inspectorWindow = inspectorWindow;
        }

        /// <summary>
        /// Verifica si un gameObject exite en la lista de objetos con Inspector
        /// </summary>
        /// <param name="gameObject">
        /// The game object.
        /// </param>
        /// <returns>
        /// Si o no esta contenido
        /// </returns>
        public static bool Contains(GameObject gameObject)
        {
            return InspectorList.ContainsKey(gameObject);
        }

        /// <summary>
        /// Obtiene el inspector de un objeto
        /// </summary>
        /// <param name="gameObject">
        /// The game object.
        /// </param>
        /// <returns>
        /// El inspector del objeto
        /// </returns>
        public static Inspector GetInspector(GameObject gameObject)
        {
            return InspectorList[gameObject];
        }

        /// <summary>
        /// Pinta el contenido del inspector
        /// </summary>
        public void OnGUI()
        {
            this.scrollPosition = EditorGUILayout.BeginScrollView(this.scrollPosition, false, false);

            foreach (var item in this.componentList)
            {
                item.Value.OnGUI();
            }

            EditorGUILayout.EndScrollView();
        }

        /// <summary>
        /// Actualiza el inspectir
        /// </summary>
        public void Refresh()
        {
            this.UpdateComponentList();
            this.inspectorWindow.Repaint();
        }

        /// <summary>
        /// Actualiza los componentes 
        /// </summary>
        private void UpdateComponentList()
        {
            if (this.gameObject == null)
            {
                return;
            }

            if (this.componentList == null)
            {
                this.componentList = new Dictionary<MonoBehaviour, ComponentEditor>();

                foreach (var component in
                    this.gameObject.GetComponents<MonoBehaviour>().Where(
                        item =>
                        item.GetType().GetCustomAttributes(true).OfType<BrowsableClassAttribute>().FirstOrDefault() !=
                        null))
                {
                    this.componentList.Add(component, new ComponentEditor(component));
                }
            }
            else
            {
                var l = new Dictionary<MonoBehaviour, ComponentEditor>();

                foreach (var component in
                   this.gameObject.GetComponents<MonoBehaviour>().Where(
                       item =>
                       item.GetType().GetCustomAttributes(true).OfType<BrowsableClassAttribute>().FirstOrDefault() !=
                       null))
                {
                    l.Add(
                        component,
                        this.componentList.ContainsKey(component) ? this.componentList[component] : new ComponentEditor(component));
                }

                this.componentList = l;
            }
        }
    }
}
